package com.GlobalPaint.app.ui.Laborer;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.GlobalPaint.app.Adapter.JobWantedAdapter;
import com.GlobalPaint.app.Base.DialogWaiting;
import com.GlobalPaint.app.Base.JobWantedBean;
import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;


public class JobWantedFragment extends Fragment {
    private JobWantedAdapter adapter;
    private int page = 1;
    private Dialog dlgWaiting2;
    private XRecyclerView job_xrecyler;
    private LinearLayoutManager linearLayoutManager;
    private JobWantedBean bean;
    private List<JobWantedBean.DataBean> list;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =inflater.inflate(R.layout.fragment_job_wanted, container, false);
        initView(view);
        return view;

    }

    private void initView(View view) {
        job_xrecyler = (XRecyclerView)view.findViewById(R.id.job_xrecyler);
        linearLayoutManager = new LinearLayoutManager(getContext());
        job_xrecyler.setLayoutManager(linearLayoutManager);
        getList();
        initLoad();
    }

    private void initLoad() {
        job_xrecyler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                String url = Constants.JASON_SERVICE_URL + "/renren-security/appapi/appJobWorkersService/query?page" + 1;
                DataManager.getInst().getHttpRequestJsonClass(url, "dd", JobWantedBean.class, new DataManager.Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        page = 1;
                        JobWantedBean Jobbean = (JobWantedBean) o;
                        if (Jobbean.getStatus() == 1) {
                            if (Jobbean.getData() != null && Jobbean.getData().size() > 0) {
                                list.clear();
                                list.addAll(Jobbean.getData());
                                adapter.notifyDataSetChanged();
                                job_xrecyler.loadMoreComplete();
                            } else {
                                Toast.makeText(getContext(), "刷新失败", Toast.LENGTH_LONG).show();
                            }
                        } else {
                            Toast.makeText(getContext(), "刷新失败", Toast.LENGTH_LONG).show();
                        }
                        job_xrecyler.refreshComplete();
                    }

                    @Override
                    public void onFailure(ReturnStatusResultEntity error) {
                        job_xrecyler.refreshComplete();
                    }
                });
            }

            @Override
            public void onLoadMore() {
                page++;
                String url = Constants.JASON_SERVICE_URL + "/renren-security/appapi/appJobWorkersService/query?" + page;
                DataManager.getInst().getHttpRequestJsonClass(url, "dd", JobWantedBean.class, new DataManager.Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        JobWantedBean jobBean = (JobWantedBean) o;
                        if (jobBean.getStatus() == 1) {
                            if (jobBean.getData() != null && jobBean.getData().size() > 0) {
                                list.addAll(jobBean.getData());
                            } else {
                                page = page - 1;
                                Toast.makeText(getContext(), "暂无更多数据", Toast.LENGTH_LONG).show();
                            }
                        } else {
                            page = page - 1;
                            Toast.makeText(getContext(), "暂无更多数据", Toast.LENGTH_LONG).show();
                        }
                        job_xrecyler.loadMoreComplete();
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(ReturnStatusResultEntity error) {
                        page = page - 1;
                        Toast.makeText(getContext(), "暂无更多数据", Toast.LENGTH_LONG).show();
                        job_xrecyler.loadMoreComplete();
                    }
                });
            }
        });
    }

    private void getList() {
        dlgWaiting2 = DialogWaiting.createDialogWaiting(getContext(), "加载中......");
        dlgWaiting2.show();
        String url= Constants.JASON_SERVICE_URL+"/renren-security/appapi/appJobWorkersService/query?page=1";
        DataManager.getInst().getHttpRequestJsonClass(url, "dd", JobWantedBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                dlgWaiting2.cancel();
                bean=(JobWantedBean)o;
                if(bean.getStatus()==1){
                    if( bean.getData()!=null){
                        list=bean.getData();
                        adapter=new JobWantedAdapter(list,getContext());
                        job_xrecyler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                }
                else {
                    Toast.makeText(getContext(),bean.getMsg(),Toast.LENGTH_LONG).show();
                }
                job_xrecyler.refreshComplete();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {
                dlgWaiting2.cancel();
                if(list!=null&&list.size()>0) {
                    list.clear();
                    Toast.makeText(getContext(), "获取失败", Toast.LENGTH_LONG).show();
                    adapter.notifyDataSetChanged();
                    job_xrecyler.refreshComplete();

                }
            }
        });
    }


}
